package com.tanguy.user.ui.main

import android.support.v4.view.ViewPager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import cn.sinata.xldutils_kotlin.activity.WebViewActivity
import com.tanguy.user.App.Companion.app
import com.tanguy.user.Const
import com.tanguy.user.R
import com.tanguy.user.base.LazyFragment
import com.tanguy.user.bean.HomeBean
import com.tanguy.user.bean.orde.OrderBean
import com.tanguy.user.custom.FrescoImageLoader
import com.tanguy.user.net.http.HttpManager
import com.tanguy.user.ui.main.adapter.TripOrderAdapter
import com.tanguy.user.ui.trip.activity.ReceivePlaneActivity
import com.tanguy.user.ui.trip.activity.ReceiveStationActivity
import com.tanguy.user.ui.trip.activity.SendPlaneActivity
import com.tanguy.user.ui.trip.activity.SendStationActivity
import com.tanguy.user.utlis.extend.click
import com.tanguy.user.utlis.extend.loge
import com.tanguy.user.utlis.extend.requestSucceed
import com.tanguy.user.utlis.extend.visibility
import com.youth.banner.Banner
import com.youth.banner.BannerConfig
import org.jetbrains.anko.find
import org.jetbrains.anko.support.v4.find
import org.jetbrains.anko.support.v4.startActivity

/**
 * Created by dab on 2017/9/30 0030 11:18
 * 出行
 */

class TripFragment : LazyFragment() {
    override fun viewLayoutID(): Int = R.layout.fragment_trip
    lateinit var banner: Banner
    lateinit var homeBean: HomeBean
    private val data=ArrayList<OrderBean>()
    private val tripOrderAdapter=TripOrderAdapter(data)
    override fun onFirstVisibleToUser(view: View?) {
        view?.apply {
            initRecycler(find<View>(R.id.rv_content) as RecyclerView)
            click(R.id.layout_receive_plane) { startActivity<ReceivePlaneActivity>() }//接机
            click(R.id.layout_send_plane) { startActivity<SendPlaneActivity>() }//送机
            click(R.id.layout_receive_station) { startActivity<ReceiveStationActivity>() }//接站
            click(R.id.layout_send_station) { startActivity<SendStationActivity>() }//送站
        }
        initBanner(view)
        banner.setOnBannerListener { position ->
            val bannerBean = homeBean.banner[position]
            //类型（0不跳转，1内部跳转，2外部跳转）
            if (bannerBean.type == 3) {
                startActivity<WebViewActivity>("url" to bannerBean.url, "title" to bannerBean.title)
            }
            if (bannerBean.type == 2) {
                startActivity<WebViewActivity>("url" to Const.Api.BASE_URL1+bannerBean.url, "title" to bannerBean.title)
            }

        }
        load()

    }


    override fun onVisibleOrInvisibleToUser(visible: Boolean) {
        super.onVisibleOrInvisibleToUser(visible)
        loge(visible)
        if (visible) {
            banner.start()
                load()
        } else {
            banner.stopAutoPlay()
        }

    }

    private fun initRecycler(recyclerView: RecyclerView) {
        recyclerView.layoutManager = LinearLayoutManager(activity)
        recyclerView.adapter = tripOrderAdapter
        recyclerView.isNestedScrollingEnabled = false
    }


    private fun load() {
        HttpManager.getTravelPage(app.user.userId)
                .requestSucceed(this) {
                    homeBean = it
                    loadBanner(it.bannerStringList)
                    tripOrderAdapter.setNewData(it.order)
                }
    }


    /**
     * 修改指示器的样式
     */
    fun upIndicator(position: Int, linearLayout: LinearLayout) {
        val pos = position % linearLayout.childCount
        for (i in 0..linearLayout.childCount) {
            linearLayout.getChildAt(i)?.find<View>(R.id.view_select)?.visibility = if (pos == i) View.VISIBLE else View.GONE
            linearLayout.getChildAt(i)?.find<View>(R.id.view_no_select)?.visibility = if (pos == i) View.GONE else View.VISIBLE
        }
    }

    private fun loadBanner(bannerList: ArrayList<String>) {
        if (bannerList.size <= 0) {
            view?.visibility(R.id.view_banner,false)
            return
        }
        view?.visibility(R.id.view_banner,true)

        //            val bannerList = arrayListOf(R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
        val linearLayout = find<LinearLayout>(R.id.layout_indicator)
        val viewPager = banner.find<ViewPager>(com.youth.banner.R.id.bannerViewPager)
        viewPager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                upIndicator(position, linearLayout)
            }
        })
        linearLayout.removeAllViews()
        for (i in 0 until bannerList.size) {
            LayoutInflater.from(context).inflate(R.layout.view_banner_indicator_trip, linearLayout)
        }
        banner.setImages(bannerList)
        banner.start()
    }

    /**
     * 初始化banner相关
     */
    private fun initBanner(view: View?) {
        view!!.apply {
            banner = find<Banner>(R.id.banner).apply {
                setBannerStyle(BannerConfig.NOT_INDICATOR)
                setImageLoader(FrescoImageLoader())
                setDelayTime(5000)
            }
            view.visibility(R.id.view_banner,false)

        }
    }
}
